package com.sduept.nwld.dataserver.manager.cim;

import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.model.param.QuotaInfoParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuotaManager {
	@Autowired
	private SuperDAO dao;
	
	
	public List<String> getGroups(){
		return (List<String>) dao.nativeQuery("select groupName from t_quota_info group by groupName");
	}
	
	public List<QuotaInfoParam> getAllQuotaInfos(){
		return dao.findAll(QuotaInfoParam.class);
	}
	public QuotaInfoParam getQuotaInfoById(String id){
		return dao.findById(id, QuotaInfoParam.class);
	}
	public List<QuotaInfoParam> getQuotaInfosByGroupName(String groupName){
		return dao.findByCondition(QuotaInfoParam.class, "groupName = ?", new Object[]{groupName});
	}
	
	public QuotaInfoParam saveOrUpdateInfo(QuotaInfoParam info){
		return dao.createOrUpdate(info, QuotaInfoParam.class);
	}

	public void deleteInfo(QuotaInfoParam currentInfo) {
		dao.delete(currentInfo.getId(), QuotaInfoParam.class);
	}

	public void updateGroupName(String from, String to) {
		List<QuotaInfoParam>infos = dao.findByCondition(QuotaInfoParam.class, "groupName = ?", from);
		for (QuotaInfoParam info : infos) {
			info.setGroupName(to);
			dao.createOrUpdate(info, QuotaInfoParam.class);
		}
	}

	public void deleteGroup(String groupName) {
		dao.delete(QuotaInfoParam.class, "groupName = ?", groupName);
	}
}
